/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


function validArtAmount(nbr)
{
    var ret = new Integer();
    // Codes d'erreur renvoyés:
    // 0 = valeur OK
    // -1 = pas un nombre
    // -2 = nombre <= 0
    // -3 = nombre trop grand
    
    
    var newNbr = new String();
    
    // On enlève d'éventuels espaces superflus'
    newNbr = newNbr.split(' ').join('');
    
    // On regarde si c'est un nombre ou pas'
    if(newNbr != parseInt(newNbr))
    {
        ret = -1;
    }        
    else
    {
        if(newNbr < 1) // Si nombre <= 0
        {
            ret = -2;
        }
        else
        {
            if (newNbr > 999) // Si nombre trop grand
            {
                ret = -3;
            }    
            else
            {
                ret = 0; // Tout est OK.
            }    
        }
    }    
    
    return ret;    
}

function getLangArray()
{
   var lngArray = new Array(3); // 1 par langue
    for (i=0; i <3; i++) // 10 elts: 8 champs + 2 msg (debut et fin de l'alert box)
        lngArray[i]=new Array(11);
   
   lngArray[0][0] = "Nom d'utilisateur (8 car. minimum)";
   lngArray[1][0] = "User name (8 char. minimum)";
   lngArray[2][0] = "Gebruikersnaam (8 letters minimum)";
   
   lngArray[0][1] = "Nom";
   lngArray[1][1] = "Last name";
   lngArray[2][1] = "Naam";

   lngArray[0][2] = "Prénom";
   lngArray[1][2] = "First name";
   lngArray[2][2] = "Voornaam";

   lngArray[0][3] = "Adresse de livraison";
   lngArray[1][3] = "Delivery address";
   lngArray[2][3] = "Afleveradres";

   lngArray[0][4] = "Adresse e-mail";
   lngArray[1][4] = "E-mail";
   lngArray[2][4] = "Email";

   lngArray[0][5] = "Numéro de téléphone";
   lngArray[1][5] = "Phone number";
   lngArray[2][5] = "Telefoon nummer";

   lngArray[0][6] = "Mot de passe (6 car. minimum)";
   lngArray[1][6] = "Password (6 char minimum)";
   lngArray[2][6] = "Wachtwoord (6 letters minimum)";

   lngArray[0][7] = "Mot de passe (répété) (6 car. minimum)";
   lngArray[1][7] = "Password (repeated) (6 char minimum)";
   lngArray[2][7] = "Wachtwoord (herhaald) (6 letters minimum)";
   
   lngArray[0][8] = "Mots de passe différents";
   lngArray[1][8] = "Passwords are not the same";
   lngArray[2][8] = "De wachtwoorden zijn niet identiek";     

   lngArray[0][9] = "Le(s) champ(s) suivants n'est/ne sont pas correctement rempli(s):";
   lngArray[1][9] = "The following field(s) is/are not correctly filled:";
   lngArray[2][9] = "De volgende veld(en) is/zijn niet correct ingevuld:";

   lngArray[0][10] = "Merci de bien vouloir le(s) corriger.";
   lngArray[1][10] = "Please correct it/them.";
   lngArray[2][10] = "Dank u vriendelijk te corrigeren.";

   return lngArray;
   
    
}


function validDataLogin(CodeLangue)
{
    var ret = new Boolean();
    ret = true;
    
    var langArray = new Array(3); // 1 par langue
    for (i=0; i <3; i++) // 11 elts: 8+1 champs + 2 msg (debut et fin de l'alert box)
        langArray[i]=new Array(11);

    langArray = getLangArray();
    
    var lngIndex;
    if (CodeLangue == "en") lngIndex = 1;
    else if (CodeLangue == "nl") lngIndex = 2;
    else lngIndex = 0; // français par défaut
    
    var incorrectFields = "";
    
    // Get data
    var username = document.forms["loginForm"]["userID"].value;
    var pw = document.forms["loginForm"]["password"].value;
    
    if(validUserID(username) == false)
    {
        ret = false;
        incorrectFields += langArray[lngIndex][0] +  "\n";
    }
    
    if(validPassword(pw) == false)
    {
        ret = false;
        incorrectFields += langArray[lngIndex][6] +  "\n";
    }
    
    if(ret == false)
        alert(langArray[lngIndex][9] + "\n\n" + incorrectFields + "\n" + langArray[lngIndex][10]);
    
    return ret;    
}


function validDataRegister(CodeLangue)
{
    var ret = new Boolean();
    ret = true;

    var langArray = new Array(3); // 1 par langue
    
    for (i=0; i <3; i++) // 11 elts: 8+1 champs + 2 msg (debut et fin de l'alert box)
        langArray[i]=new Array(11);

    langArray = getLangArray();

    var lngIndex;
    if (CodeLangue == "en") lngIndex = 1;
    else if (CodeLangue == "nl") lngIndex = 2;
    else lngIndex = 0; // français par défaut
    
    var incorrectFields = "";

    // Get data
    var username = document.forms["userDataForm"]["userID"].value;
    var firstName = document.forms["userDataForm"]["firstName"].value;
    var lastName = document.forms["userDataForm"]["lastName"].value;
    var delivAddr = document.forms["userDataForm"]["deliveryAddress"].value;
    var email = document.forms["userDataForm"]["email"].value;
    var phoneNbr = document.forms["userDataForm"]["phoneNumber"].value;
    var pw1 = document.forms["userDataForm"]["password"].value;
    var pw2 = document.forms["userDataForm"]["passwordRepeat"].value;

    if(validUserID(username) == false)
    {
        ret = false;
        incorrectFields += langArray[lngIndex][0] +  "\n";
    }    
    
    if(validLastName(lastName) == false)
    {
        ret = false;
        incorrectFields += langArray[lngIndex][1] + "\n";
    }    
    
    if(validFirstName(firstName) == false)
    {
        ret = false;
        incorrectFields += langArray[lngIndex][2] + "\n";        
    }
    
    if(validSupplyAddress(delivAddr) == false)
    {
        ret = false;
        incorrectFields += langArray[lngIndex][3] + "\n";        
    }
    
    if(validEmail(email) == false)
    {
        ret = false;
        incorrectFields += langArray[lngIndex][4] + "\n";        
    }
        
     if(validPhoneNumber(phoneNbr) == false)
     {
         ret = false;
         incorrectFields += langArray[lngIndex][5] + "\n";
     }
    
     if(validPassword(pw1) == false)
     {
         ret = false;
         incorrectFields += langArray[lngIndex][6] + "\n";         
     }
     
     if(validPassword(pw2) == false)
     {
         ret = false;
         incorrectFields += langArray[lngIndex][7] + "\n";         
     }
    
     if(comparePasswords(pw1, pw2) == false)
     {
         ret = false;
         incorrectFields += langArray[lngIndex][8] + "\n"; 
     }
    
    if(ret == false)
        alert(langArray[lngIndex][9] + "\n\n" + incorrectFields + "\n" + langArray[lngIndex][10]);

    return ret;    
}

function validEmail(address)
{
    var ret = new Boolean(true);
    // Si ret = true, adresse OK
    // Sinon ret = false;
    
    var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
    // REGEX, permet de vérifier l'e-mail'
    
    ret = ((address.length != 0) && reg.test(address));
    
    return ret;    
}

function validUserID(usrid)
{
    var newUID = new String();
    newUID = usrid;
    newUID = usrid.split(' ').join('');
    if(newUID.length == 0) return false;
    else return true;
    
    return (usrid.length  >= 6);
}

function validPassword(pw)
{
    return (pw.length >= 6);
}

function comparePasswords(pw1, pw2)
{
    return (pw1 == pw2);
}

function validPhoneNumber(pnbr)
{
    var ret = new Boolean();
    var reg = /^(\\+[0-9][0-9][0-9]( [0-9][0-9])+)|([0-9]+)$/;
    ret = ((pnbr.length != 0) && reg.test(pnbr));
    return ret;
}

// valid prénom
function validFirstName(fn) 
{
    var newFN = new String();
    newFN = fn;
    newFN = newFN.split(' ').join('');
    if(newFN.length == 0) return false;
    else return true;
}

// valid nom
function validLastName(ln)
{
    var newLN = new String();
    newLN = ln
    newLN = newLN.split(' ').join('');
    if(newLN.length == 0) return false;
    else return true;
}

// valid adr livraison
function validSupplyAddress(sa)
{
    var newSA = new String();
    newSA = sa
    newSA = newSA.split(' ').join('');
    if(newSA.length == 0) return false;
    else return true;
}

